package com.unicom.priceboard.data.vo;

import java.util.HashMap;
import com.google.gson.annotations.Expose;
import com.unicom.priceboard.util.HTML_Producer;
import com.unicom.priceboard.util.NumberUtil;

public class HaStock {

	private String centerId;
	private String symbol;
	private String tradingDate;
	public String share_Dividend = "0";
	private int stockId;
	private int stockType;
	private String stock_name;
	private double openPrice;
	private double closePrice;
	private double averagePrice;
	private double priorClosePrice;
	private double ceiling;
	private double floor;
	private double highestPrice;
	private double lowestPrice;
	//Continuous trade only
	private int totalVol;
	private double best3Bid;
	private int best3BidVolume;
	private double best2Bid;
	private int best2BidVolume;
	private double best1Bid;
	private int best1BidVolume;
	private double price;
	private double prevPrice;
	private int vol;
	private double best1Offer;
	private int best1OfferVolume;
	private double best2Offer;
	private int best2OfferVolume;
	private double best3Offer;
	private int best3OfferVolume;
	private double totalTradingValue;
	private long totalTradingVolume;
	private double pct_change;
	private double change;
	private String time;
	private int totalDeal;
	private int foreignBuyVol;
	private double foreignBuyVal;
	private int foreignSellVol;
	private double foreignSellVal;
	private double ptdTotalValue;
	private int ptdTotalVolume;
//	private int totalBuyVol;
//	private double totalBuyVal;
//	private int totalSellVol;
//	private double totalSellVal;
	
	public int getTotalDeal() {
		return totalDeal;
	}

	public void setTotalDeal(int totalDeal) {
		this.totalDeal = totalDeal;
	}

	/*these changes is for after trading time*/
	private double close_change;
	private double close_pct_Change;
	
	/*Indicator for the +/- column*/
	@Expose
	String indicator;
	
	//Stylesheet map for properties
	@Expose
	HashMap<String, String> mapCss = new HashMap<String, String>();
	
	//formated value sent to clients
	@Expose
	HashMap<String, String> mapValues = new HashMap<String, String>();
	
	public HaStock(){
		/*Initialize default formated values*/
		//gia khop lenh
		mapValues.put("price", "");
		//khoi luong ung voi gia khop lenh
		mapValues.put("vol", "");
		mapValues.put("totalVol", "");
		mapValues.put("change", "");
		mapValues.put("priorClosePrice", "");
		mapValues.put("ceiling", "");
		mapValues.put("floor", "");
		mapValues.put("highestPrice", "");
		mapValues.put("lowestPrice", "");
		mapValues.put("best1Bid", "");
		mapValues.put("best2Bid", "");
		mapValues.put("best3Bid", "");
		mapValues.put("best1BidVolume", "");
		mapValues.put("best2BidVolume", "");
		mapValues.put("best3BidVolume", "");
		mapValues.put("best1Offer", "");
		mapValues.put("best2Offer", "");
		mapValues.put("best3Offer", "");
		mapValues.put("best1OfferVolume", "");
		mapValues.put("best2OfferVolume", "");
		mapValues.put("best3OfferVolume", "");
	}
	
	public HaStock(String symbol) {
		this.symbol = symbol;
	}
	/**
	 * @return the symbol_code
	 */
	public String getSymbol() {
		return symbol;
	}

	/**
	 * @param symbol_code the symbol_code to set
	 */
	public void setSymbol(String symbol) {
		this.symbol = symbol;
		this.mapValues.put("symbol", this.symbol);
	}
	
	public int getStockId() {
		return stockId;
	}
	
	public void setStockId(int stockId) {
		this.stockId = stockId;
	}
	
	/**
	 * @return the best1Bid
	 */
	public double getBest1Bid() {
		return best1Bid;
	}

	/**
	 * @param best1Bid the best1Bid to set
	 */
	public void setBest1Bid(double best1Bid) {
		this.best1Bid = best1Bid;
	}

	/**
	 * @return the best1BidVolume
	 */
	public int getBest1BidVolume() {
		return best1BidVolume;
	}

	/**
	 * @param best1BidVolume the best1BidVolume to set
	 */
	public void setBest1BidVolume(int best1BidVolume) {
		this.best1BidVolume = best1BidVolume;
	}

	/**
	 * @return the best1Offer
	 */
	public double getBest1Offer() {
		return best1Offer;
	}

	/**
	 * @param best1Offer the best1Offer to set
	 */
	public void setBest1Offer(double best1Offer) {
		this.best1Offer = best1Offer;
	}

	/**
	 * @return the best1OfferVolume
	 */
	public int getBest1OfferVolume() {
		return best1OfferVolume;
	}

	/**
	 * @param best1OfferVolume the best1OfferVolume to set
	 */
	public void setBest1OfferVolume(int best1OfferVolume) {
		this.best1OfferVolume = best1OfferVolume;
	}

	/**
	 * @return the best2Bid
	 */
	public double getBest2Bid() {
		return best2Bid;
	}

	/**
	 * @param best2Bid the best2Bid to set
	 */
	public void setBest2Bid(double best2Bid) {
		this.best2Bid = best2Bid;
	}

	/**
	 * @return the best2BidVolume
	 */
	public int getBest2BidVolume() {
		return best2BidVolume;
	}

	/**
	 * @param best2BidVolume the best2BidVolume to set
	 */
	public void setBest2BidVolume(int best2BidVolume) {
		this.best2BidVolume = best2BidVolume;
	}

	/**
	 * @return the best2Offer
	 */
	public double getBest2Offer() {
		return best2Offer;
	}

	/**
	 * @param best2Offer the best2Offer to set
	 */
	public void setBest2Offer(double best2Offer) {
		this.best2Offer = best2Offer;
	}

	/**
	 * @return the best2OfferVolume
	 */
	public int getBest2OfferVolume() {
		return best2OfferVolume;
	}

	/**
	 * @param best2OfferVolume the best2OfferVolume to set
	 */
	public void setBest2OfferVolume(int best2OfferVolume) {
		this.best2OfferVolume = best2OfferVolume;
	}

	/**
	 * @return the best3Bid
	 */
	public double getBest3Bid() {
		return best3Bid;
	}

	/**
	 * @param best3Bid the best3Bid to set
	 */
	public void setBest3Bid(double best3Bid) {
		this.best3Bid = best3Bid;
		
	}

	/**
	 * @return the best3BidVolume
	 */
	public int getBest3BidVolume() {
		return best3BidVolume;
	}

	/**
	 * @param best3BidVolume the best3BidVolume to set
	 */
	public void setBest3BidVolume(int best3BidVolume) {
		this.best3BidVolume = best3BidVolume;
	}

	/**
	 * @return the best3Offer
	 */
	public double getBest3Offer() {
		return best3Offer;
	}

	/**
	 * @param best3Offer the best3Offer to set
	 */
	public void setBest3Offer(double best3Offer) {
		this.best3Offer = best3Offer;
	}

	/**
	 * @return the best3OfferVolume
	 */
	public int getBest3OfferVolume() {
		return best3OfferVolume;
	}

	/**
	 * @param best3OfferVolume the best3OfferVolume to set
	 */
	public void setBest3OfferVolume(int best3OfferVolume) {
		this.best3OfferVolume = best3OfferVolume;
	}

	/**
	 * @return the ceiling
	 */
	public double getCeiling() {
		return ceiling;
	}

	/**
	 * @param ceiling the ceiling to set
	 */
	public void setCeiling(double ceiling) {
		this.ceiling = ceiling;
		this.mapValues.put("ceiling", NumberUtil.formatNumber(ceiling/HTML_Producer.hastc_unit));
	}

	/**
	 * @return the change
	 */
	public double getChange() {
		return this.change;
	}

	/**
	 * @param change the change to set
	 */
//	public void setChange(String change) {
//		this.change = change;
//	}

	/**
	 * @return the floor
	 */
	public double getFloor() {
		return floor;
	}

	/**
	 * @param floor the floor to set
	 */
	public void setFloor(double floor) {
		this.floor = floor;
		this.mapValues.put("floor", NumberUtil.formatNumber(floor/HTML_Producer.hastc_unit));
	}

	/**
	 * @return the price
	 */
	public double getPrice() {
		return price;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrevPrice(double price) {
		this.prevPrice = price;
	}

	public double getPrevPrice() {
		return prevPrice;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(double price) {
		this.price = price;
		
		//Set the CSS and indicator
		mapCss.put("price", getStyleSheet(price, ""));
		this.indicator = identifyIndicator(mapCss.get("price"));
		this.mapValues.put("price", NumberUtil.formatNumber(price/HTML_Producer.hastc_unit));
	}

	/**
	 * @return the total volume
	 */
	public int getTotalVol() {
		return totalVol;
	}
	
	public void setTotalVol(int vol) {
		this.totalVol = vol;
		mapValues.put("totalVol", NumberUtil.formatVolume(totalVol));
	}

	/**
	 * @return the priorClosePrice
	 */
	public double getPriorClosePrice() {
		return priorClosePrice;
	}

	/**
	 * @param priorClosePrice the priorClosePrice to set
	 */
	public void setPriorClosePrice(double priorClosePrice) {
		this.priorClosePrice = priorClosePrice;
		this.mapValues.put("priorClosePrice", NumberUtil.formatNumber(priorClosePrice/HTML_Producer.hastc_unit));
	}

	/**
	 * @return the vol
	 */
	public int getVol() {
		return vol;
	}

	/**
	 * @param vol the vol to set
	 */
	public void setVol(int vol) {
		this.vol = vol;
		this.mapValues.put("vol", NumberUtil.formatVolume(vol));
	}
	
	/**
	 * @param stockType the stockType to set
	 */
	public void setStockType(int stockType) {
		this.stockType = stockType;
	}

	/**
	 * @return the stockType
	 */
	public int getStockType() {
		return stockType;
	}

	/**
	 * @param tradingDate the tradingDate to set
	 */
	public void setTradingDate(String tradingDate) {
		this.tradingDate = tradingDate;
	}

	/**
	 * @return the tradingDate
	 */
	public String getTradingDate() {
		return tradingDate;
	}

	/**
	 * @param averagePrice the averagePrice to set
	 */
	public void setAveragePrice(double averagePrice) {
		this.averagePrice = averagePrice;
	}

	/**
	 * @return the averagePrice
	 */
	public double getAveragePrice() {
		return averagePrice;
	}
	
	public void setToPreviousDay() {
		this.setPriorClosePrice(this.getAveragePrice());
		double ceil = this.getPriorClosePrice() + (double)(this.getPriorClosePrice()*0.1);
		this.setCeiling(((double)(ceil/100))*100);
		double floor = this.getPriorClosePrice() - (double)(this.getPriorClosePrice()*0.1);
		this.setFloor(((double)(floor/100))*100);		 
		this.setPrice(0);
		this.setVol(0);
		this.setTotalVol(0);
	}

	public String toString() {
		return this.symbol + " --- " + this.stockId + "---" + this.tradingDate;
	}

	/**
	 * @param centerId the centerId to set
	 */
	public void setCenterId(String centerId) {
		this.centerId = centerId;
	}

	/**
	 * @return the centerId
	 */
	public String getCenterId() {
//		return String.valueOf(Constants.HANOI_CENTER_ID);
		return this.centerId;
	}

	/**
	 * @return the highestPrice
	 */
	public double getHighestPrice() {
		return highestPrice;
	}

	/**
	 * @param highestPrice the highestPrice to set
	 */
	public void setHighestPrice(double highestPrice) {
		this.highestPrice = highestPrice;
		
		//set the CSS and the output Value
		mapCss.put("highestPrice", getStyleSheet(highestPrice, "highlow_"));
		mapValues.put("highestPrice", NumberUtil.formatNumber(highestPrice/HTML_Producer.hastc_unit));
	}

	/**
	 * @return the lowestPrice
	 */
	public double getLowestPrice() {
		return lowestPrice;
	}

	/**
	 * @param lowestPrice the lowestPrice to set
	 */
	public void setLowestPrice(double lowestPrice) {
		this.lowestPrice = lowestPrice;
		
		//set the CSS and the output Value
		mapCss.put("lowestPrice", getStyleSheet(lowestPrice, "highlow_"));
		mapValues.put("lowestPrice", NumberUtil.formatNumber(lowestPrice/HTML_Producer.hastc_unit));
	}

	public double getTotalTradingValue() {
		return totalTradingValue;
	}

	public void setTotalTradingValue(double totalTradingValue) {
		this.totalTradingValue = totalTradingValue;
	}

	/**
	 * @return the pct_change
	 */
	public double getPct_change() {
		return pct_change;
	}

	/**
	 * @param pct_change the pct_change to set
	 */
	public void setPct_change(double pct_change) {
		this.pct_change = pct_change;
	}

	/**
	 * @return the stock_name
	 */
	public String getStock_name() {
		return stock_name;
	}

	/**
	 * @param stock_name the stock_name to set
	 */
	public void setStock_name(String stock_name) {
		this.stock_name = stock_name;
	}

	@Override
	public boolean equals(Object obj) {
		if(!this.getClass().isInstance(obj))
			return false;
		else{
			HaStock compareObject = (HaStock) obj;
			if(priorClosePrice!=compareObject.getPriorClosePrice()) {
				return false;
			}else if(price!=compareObject.getPrice()) {
				return false;
			}else if(vol!=compareObject.getVol()) {
				return false;
			}else if(totalVol!=compareObject.getTotalVol()) {
				return false;
			}else if(highestPrice!=compareObject.getHighestPrice()) {
				return false;
			}else if(lowestPrice!=compareObject.getLowestPrice()) {
				return false;
			}else if(best1Bid!=compareObject.getBest1Bid()) {
				return false;
			}else if(best1BidVolume!=compareObject.getBest1BidVolume()) {
				return false;
			}else if(best2Bid!=compareObject.getBest2Bid()) {
				return false;
			}else if(best2BidVolume!=compareObject.getBest2BidVolume()) {
				return false;
			}else if(best3Bid!=compareObject.getBest3Bid()) {
				return false;
			}else if(best3BidVolume!=compareObject.getBest3BidVolume()) {
				return false;
			}else if(best1Offer!=compareObject.getBest1Offer()) {
				return false;
			}else if(best1OfferVolume!=compareObject.getBest1OfferVolume()) {
				return false;
			}else if(best2Offer!=compareObject.getBest2Offer()) {
				return false;
			}else if(best2OfferVolume!=compareObject.getBest2OfferVolume()) {
				return false;
			}else if(best3Offer!=compareObject.getBest3Offer()) {
				return false;
			}else if(best3OfferVolume!=compareObject.getBest3OfferVolume()) {
				return false;
			}
			
			return true;
		}
	}
	
	/**
	 * This method is used for set the Best bids and Best Offers for a stock
	 * @param bestPrice
	 */
	public void setBestPrice(BestPriceInfo bestPrice) {
		this.best1Bid = bestPrice.getBestBuy1Price();
		this.best1BidVolume = bestPrice.getBestBuy1Vol();
		this.best2Bid = bestPrice.getBestBuy2Price();
		this.best2BidVolume = bestPrice.getBestBuy2Vol();
		this.best3Bid = bestPrice.getBestBuy3Price();
		this.best3BidVolume = bestPrice.getBestBuy3Vol();
		this.best1Offer = bestPrice.getBestOffer1Price();
		this.best1OfferVolume = bestPrice.getBestOffer1Vol();
		this.best2Offer = bestPrice.getBestOffer2Price();
		this.best2OfferVolume = bestPrice.getBestOffer2Vol();
		this.best3Offer = bestPrice.getBestOffer3Price();
		this.best3OfferVolume = bestPrice.getBestOffer3Vol();
		
		/*Set the stylesheet for Bid/Ask price*/
		mapCss.put("best1Bid", getStyleSheet(best1Bid, ""));
		mapCss.put("best2Bid", getStyleSheet(best2Bid, ""));
		mapCss.put("best3Bid", getStyleSheet(best3Bid, ""));
		mapCss.put("best1Offer", getStyleSheet(best1Offer, ""));
		mapCss.put("best2Offer", getStyleSheet(best2Offer, ""));
		mapCss.put("best3Offer", getStyleSheet(best3Offer, ""));
		
		/*Set the formated Values*/
		this.mapValues.put("best1Bid", NumberUtil.formatNumber(best1Bid/HTML_Producer.hastc_unit));
		this.mapValues.put("best2Bid", NumberUtil.formatNumber(best2Bid/HTML_Producer.hastc_unit));
		this.mapValues.put("best3Bid", NumberUtil.formatNumber(best3Bid/HTML_Producer.hastc_unit));
		this.mapValues.put("best1Offer", NumberUtil.formatNumber(best1Offer/HTML_Producer.hastc_unit));
		this.mapValues.put("best2Offer", NumberUtil.formatNumber(best2Offer/HTML_Producer.hastc_unit));
		this.mapValues.put("best3Offer", NumberUtil.formatNumber(best3Offer/HTML_Producer.hastc_unit));
		this.mapValues.put("best1BidVolume", NumberUtil.formatVolume(best1BidVolume));
		this.mapValues.put("best2BidVolume", NumberUtil.formatVolume(best2BidVolume));
		this.mapValues.put("best3BidVolume", NumberUtil.formatVolume(best3BidVolume));
		this.mapValues.put("best1OfferVolume", NumberUtil.formatVolume(best1OfferVolume));
		this.mapValues.put("best2OfferVolume", NumberUtil.formatVolume(best2OfferVolume));
		this.mapValues.put("best3OfferVolume", NumberUtil.formatVolume(best3OfferVolume));
	}

	/**
	 * @param change the change to set
	 */
	public void setChange(double change) {
		this.change = change;
		String numberSign = change>0?"+":"";
		this.mapValues.put("change", numberSign+NumberUtil.formatOneDigit(String.valueOf(change)));
	}

	/**
	 * @return the openPrice
	 */
	public double getOpenPrice() {
		return openPrice;
	}

	/**
	 * @param openPrice the openPrice to set
	 */
	public void setOpenPrice(double openPrice) {
		this.openPrice = openPrice;
	}

	/**
	 * @return the closePrice
	 */
	public double getClosePrice() {
		return closePrice;
	}

	/**
	 * @param closePrice the closePrice to set
	 */
	public void setClosePrice(double closePrice) {
		this.closePrice = closePrice;
	}

	/**
	 * @return the close_change
	 */
	public double getClose_change() {
		return close_change;
	}

	/**
	 * @param close_change the close_change to set
	 */
	public void setClose_change(double close_change) {
		this.close_change = close_change;
	}

	/**
	 * @return the close_pct_Change
	 */
	public double getClose_pct_Change() {
		return close_pct_Change;
	}

	/**
	 * @param close_pct_Change the close_pct_Change to set
	 */
	public void setClose_pct_Change(double close_pct_Change) {
		this.close_pct_Change = close_pct_Change;
	}

	/**
	 * @return the indicator
	 */
	public String getIndicator() {
		return indicator;
	}

	/**
	 * @param indicator the indicator to set
	 */
	public void setIndicator(String indicator) {
		this.indicator = indicator;
	}

	/**
	 * @return the mapCss
	 */
	public HashMap<String, String> getMapCss() {
		return mapCss;
	}
	
	/**
	 * @param price
	 * @param prefix
	 * @return stylesheet for specified price
	 */
	private String getStyleSheet(double stockPrice,String prefix) {
		String retCSS = "";
		
		//only compare if price # 0
		if(stockPrice==0)
			return retCSS;
		
		if(this.priorClosePrice!=0) {
			if(this.share_Dividend.equals("0")) {
				if(stockPrice>this.priorClosePrice)
					retCSS = prefix + (stockPrice==this.ceiling?"hitCeil":"gainer");
				else if(stockPrice==this.priorClosePrice)
					retCSS = prefix + "unchange";
				else
					retCSS = prefix + (stockPrice==floor?"hitFloor":"loser");
			}else {
				double newRefPrice = (this.ceiling+this.floor)/2;
				if(stockPrice>newRefPrice)
					retCSS = prefix + (stockPrice==this.ceiling?"hitCeil":"gainer");
				else if(stockPrice==newRefPrice)
					retCSS = prefix + "unchange";
				else
					retCSS = prefix + (stockPrice==floor?"hitFloor":"loser");
			}
		}else 
			retCSS="hastcNew";
		
		return retCSS;
	}
	
	/**
	 * @param css
	 * @return indicator of specified stylesheet
	 */
	private String identifyIndicator(String css) {
		String retVal = "";
		if(css.contains("gainer"))
			retVal="images/up.gif";
		else if(css.contains("loser"))
			retVal="images/down.gif";
		else if(css.contains("hitCeil"))
			retVal="images/up_ceil.gif";
		else if(css.contains("hitFloor"))
			retVal="images/down_floor.gif";
		else if(css.contains("unchange") || css.equals(""))
			retVal="";
		return retVal;
	}

	/**
	 * @return the mapValues
	 */
	public HashMap<String, String> getMapValues() {
		return mapValues;
	}

	/**
	 * @param mapValues the mapValues to set
	 */
	public void setMapValues(HashMap<String, String> mapValues) {
		this.mapValues = mapValues;
	}

	public String getShare_Dividend() {
		return share_Dividend;
	}

	public void setShare_Dividend(String share_Dividend) {
		this.share_Dividend = share_Dividend;
		if(share_Dividend.equals("1")) {
			double newRefPrice = (this.ceiling+this.floor)/2;
			String priceCSS = "";
			
			if(this.price>newRefPrice)
				priceCSS = (this.price==this.ceiling?"hitCeil":"gainer");
			else if(this.price==newRefPrice)
				priceCSS =  "unchange";
			else
				priceCSS = (this.price==floor?"hitFloor":"loser");
			
			this.indicator = identifyIndicator(priceCSS);
			this.mapCss.put("price", priceCSS);
			
			//reset CSS
			mapCss.put("highestPrice", getStyleSheet(highestPrice, "highlow_"));
			mapCss.put("lowestPrice", getStyleSheet(lowestPrice, "highlow_"));
			mapCss.put("best1Bid", getStyleSheet(best1Bid, ""));
			mapCss.put("best2Bid", getStyleSheet(best2Bid, ""));
			mapCss.put("best3Bid", getStyleSheet(best3Bid, ""));
			mapCss.put("best1Offer", getStyleSheet(best1Offer, ""));
			mapCss.put("best2Offer", getStyleSheet(best2Offer, ""));
			mapCss.put("best3Offer", getStyleSheet(best3Offer, ""));
		}
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public int getForeignBuyVol() {
		return foreignBuyVol;
	}

	public void setForeignBuyVol(int foreignBuyVol) {
		this.foreignBuyVol = foreignBuyVol;
	}
	
	public double getForeignBuyVal() {
		return foreignBuyVal;
	}

	public void setForeignBuyVal(double foreignBuyVal) {
		this.foreignBuyVal = foreignBuyVal;
	}

	public int getForeignSellVol() {
		return foreignSellVol;
	}

	public void setForeignSellVol(int foreignSellVol) {
		this.foreignSellVol = foreignSellVol;
	}

	public double getForeignSellVal() {
		return foreignSellVal;
	}

	public void setForeignSellVal(double foreignSellVal) {
		this.foreignSellVal = foreignSellVal;
	}

	public double getPtdTotalValue() {
		return ptdTotalValue;
	}

	public void setPtdTotalValue(double ptdTotalValue) {
		this.ptdTotalValue = ptdTotalValue;
	}

	public int getPtdTotalVolume() {
		return ptdTotalVolume;
	}

	public void setPtdTotalVolume(int ptdTotalVolume) {
		this.ptdTotalVolume = ptdTotalVolume;
	}

	public long getTotalTradingVolume() {
		return totalTradingVolume;
	}

	public void setTotalTradingVolume(long totalTradingVolume) {
		this.totalTradingVolume = totalTradingVolume;
	}
	
	/*public int getTotalBuyVol() {
		return totalBuyVol;
	}

	public void setTotalBuyVol(int totalBuyVol) {
		this.totalBuyVol = totalBuyVol;
	}

	public double getTotalBuyVal() {
		return totalBuyVal;
	}

	public void setTotalBuyVal(double totalBuyVal) {
		this.totalBuyVal = totalBuyVal;
	}

	public int getTotalSellVol() {
		return totalSellVol;
	}

	public void setTotalSellVol(int totalSellVol) {
		this.totalSellVol = totalSellVol;
	}

	public double getTotalSellVal() {
		return totalSellVal;
	}

	public void setTotalSellVal(double totalSellVal) {
		this.totalSellVal = totalSellVal;
	}
*/	
}
